import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-02-13
 * Time: 10:15
 */
class Solution13 {
    public int findUnsortedSubarray(int[] nums) {
        int n = nums.length;
        int[] tmp = new int[n];
        tmp = Arrays.copyOf(nums, n);
        Arrays.sort(tmp);
        int left = 0, right = 0;
        int ret = Integer.MIN_VALUE;
        while (left < n && nums[left] == tmp[left]) {
            left++;
        }
        right = left + 1;
        while (right < n) {
            while (right < n && nums[right] == tmp[right]) {
                right++;
            }
            if(right < n){
                ret = Math.max(ret, right - left + 1);
            }
            right++;
        }
        if(ret == Integer.MIN_VALUE) return 0;
        return ret;
    }
}
public class Test13 {
    public static void main(String[] args) {
        Solution13 solution13 = new Solution13();
        int[] nums = new int[]{2,6,4,8,10,9,15};
        solution13.findUnsortedSubarray(nums);
    }
}
